Certified Java Programmer Mock Exam


Question 1



Which of the following are modifiers that can be applied to a method declaration within an interface?

a. static
b. synchronized
c. transient
d. volatile
e. strictfp
f. native
g. None of the above.

Question 2



Which of the following are modifiers that can be applied to a field declaration within an interface?

a. static
b. synchronized
c. transient
d. volatile
e. strictfp
f. None of the above.

Question 3



Which of the following are modifiers that can be applied to an interface that is not a member of an enclosing class?

a. static
b. synchronized
c. transient
d. volatile
e. strictfp
f. None of the above.

Question 4


interface A {
  void m1();              // 1
  public void m2();       // 2
  protected void m3();    // 3
  private void m4();      // 4
  abstract void m5();     // 5
}

Compile-time errors are generated at which lines?

a. 1
b. 2
c. 3
d. 4
e. 5
f. None of the above.

Question 5


interface A {void main(String[] args);}               // 1
interface B {public void main(String[] args);}        // 2
interface C {public static void main(String[] args);} // 3
interface D {protected void main(String[] args);}     // 4
interface E {private void main(String[] args);}       // 5

Which interface declarations generate a compile-time error?

a. 1
b. 2
c. 3
d. 4
e. 5
f. None of the above.

Question 6


interface I {String s1 = "I";}
class A implements I {String s1 = "A";}
class B extends A {String s1 = "B";}
class C extends B {
  String s1 = "C";
  void printIt() {
    System.out.print(((A)this).s1 + ((B)this).s1 +
                     ((C)this).s1 + ((I)this).s1);
  }
  public static void main (String[] args) {
    new C().printIt();
  }
}

What is the result of attempting to compile and run the above program?

a. Prints: ABCI
b. Runtime Exception
c. Compiler Error
d. None of the Above

Question 7


1.  interface I10 {
2.    String name = "I10";
3.    String s10 = "I10.s10";
4.  }
5.  interface I20 {
6.    String name = "I20";
7.    String s20 = "I20.s20";
8.  }
9.  class C10 implements I10, I20 {
10.   public static void main(String[] args) {
11.     System.out.print(s10+",");
12.     System.out.print(s20+",");
13.     System.out.print(name);
14.   }
15. }

What is the result of attempting to compile and run the above program?

a. Prints: I10.s10,I20.s20,I10
b. Prints: I10.s10,I20.s20,I20
c. Prints: I10.s10,I20.s20,
d. Prints: I10.s10,I20.s20,null
e. Compiler error at line 9.
f. Compiler error at line 11.
g. Compiler error at line 12.
h. Compiler error at line 13.
i. Runtime Error
j. None of the Above

Question 8


class A {}
class B extends A {}
class C extends B {
  static void m(A x, A y) {System.out.print("AA");}
  static void m(A x, B y) {System.out.print("AB");}
  static void m(B x, A y) {System.out.print("BA");}
  static void m(B x, B y) {System.out.print("BB");}
  static void m(A x, C y) {System.out.print("AC");}
  public static void main(String[] args) {
    C c = new C();
    m(c, c);
  }
}

What is the result of attempting to compile and run the above program?

a. Prints: AA
b. Prints: AB
c. Prints: BA
d. Prints: BB
e. Prints: AC
f. Compiler error.
g. Runtime error.
h. None of the Above

Question 9


class A {}
class B extends A {}
class C extends B {
  static void m1(A x) {System.out.print("m1A");}
  static void m2(B x) {System.out.print("m2B"); m1(x);}
  static void m2(A x) {System.out.print("m2A"); m1(x);}
  static void m3(C x) {System.out.print("m3C"); m2(x);}
  static void m3(B x) {System.out.print("m3B"); m2(x);}
  static void m3(A x) {System.out.print("m3A"); m2(x);}
  public static void main(String[] args) {
    m3(new C());
  }
}

What is the result of attempting to compile the program?

a. Prints: m3Am2Am1A
b. Prints: m3Bm2Bm1A
c. Prints: m3Cm2Bm1A
d. Prints: m3Cm2Am1A
e. Compiler error
f. Run time error
g. None of the Above

Question 10


class M {
  static String s1 = "M.s1";
  static void printS1(){System.out.print("M.printS1,"+s1+" ");}
  M() { printS1();}
}

class N extends M {
  static String s1 = "N.s1";
  static void printS1(){System.out.print("N.printS1,"+s1+" ");}
  N() { printS1();}
  public static void main(String[] args) {
    N n = new N();
  }
}

What is the result of attempting to compile and run the above program?

a. Prints: M.printS1,M.s1 N.printS1,N.s1
b. Prints: N.printS1,N.s1 N.printS1,N.s1
c. Prints: N.printS1,N.s1 M.printS1,M.s1
d. Prints: N.printS1,M.s1 M.printS1,M.s1
e. Prints: N.printS1,N.s1
f. Runtime Exception
g. Compiler Error
h. None of the Above

Question 11


class A {}
class B extends A {}
class C {
  void m(A x) {System.out.print("A");}
  void m(B x) {System.out.print("B");}
  public static void main(String[] args) {
    C c = new C();
    c.m(null);
  }
}

What is the result of attempting to compile and run the above program?

a. Prints: A
b. Prints: B
c. Compiler error.
d. Runtime error.
e. None of the Above

Question 12


abstract class D {
  String s1 = "D";
  abstract String getS1();
}
class E extends D {
  String s1 = "E";
  String getS1() {return s1;}
}
class F {
  public static void main (String[] s) {
    D x = new E();
    System.out.print(x.s1 + x.getS1());
  }
}

What is the result of attempting to compile and run the above program?

a. Prints: DD
b. Prints: DE
c. Prints: ED
d. Prints: EE
e. Runtime Error
f. Compiler Error
g. None of the Above

Question 13


class G {
  String s1 = "G.s1";
  void printS1(){System.out.print("G.printS1," + s1);}
  G() { printS1();}
}

class H extends G {
  String s1 = "H.s1";
  void printS1(){System.out.print("H.printS1," + s1);}
  public static void main(String[] args) {
    H h = new H();
  }
}

What is the result of attempting to compile and run the above program?

a. Prints: G.printS1,G.s1
b. Prints: G.printS1,H.s1
c. Prints: G.printS1,null
d. Prints: H.printS1,G.s1
e. Prints: H.printS1,H.s1
f. Prints: H.printS1,null
g. Runtime Exception
h. Compiler Error
i. None of the Above

Question 14


class C {
  void printS1() {System.out.print("C.printS1 ");}
  static void printS2() {System.out.print("C.printS2 ");}
}

class D extends C {
  void printS1(){System.out.print("D.printS1 ");}
  void printS2() {System.out.print("D.printS2 ");}
  public static void main (String args[]) {
    C c = new D();
    c.printS1(); c.printS2();
  }
}

What is the result of attempting to compile and run the above program?

a. Prints: C.printS1 C.printS2
b. Prints: C.printS1 D.printS2
c. Prints: D.printS1 C.printS2
d. Prints: D.printS1 D.printS2
e. Runtime error
f. Compiler error
g. None of the above

Question 15


class A {void m1() {System.out.print("A");}}
class B extends A {void m1(){System.out.print("B");}}
class C extends B {void m1() {System.out.print("C");}}
class D extends C {
  void m1() {System.out.print("D");}
  void m2() {
    m1();
    ((C)this).m1(); // 1
    ((B)this).m1(); // 2
    ((A)this).m1(); // 3
  }
  public static void main (String[] args) {
    new D().m2(); // 4
  }
}

What is the result of attempting to compile and run the above program?

a. Prints: DCBA
b. Prints: DDDD
c. Compiler Error at 1.
d. Compiler Error at 2.
e. Compiler Error at 3.
f. Compiler Error at 4.
g. Runtime Error
h. None of the Above

Question 16


interface A {
  String m1();
  class B implements A {public String m1() {return "B";}}
}
class E {
  public static void main(String[] args) {
    // Insert statement here.
  }
}

Which of the following statements could be inserted into the program to print the String ""B"?

a. System.out.print(m1());
b. System.out.print(B.m1());
c. System.out.print(A.B.m1());
d. System.out.print(new B().m1());
e. System.out.print(new A.B().m1());
f. System.out.print(new A().new B().m1());
g. Run"time Error
h. Compile"time Error
i. None of the Above

Question 17


class A {String s1="A";}
class B extends A {String s1="B";}
class C extends B {String s1="C";}
class D extends C {
  String s1="D";
  void m1() {
    System.out.print(s1  + ",");
    System.out.print(((C)this).s1  + ","); // 1
    System.out.print(((B)this).s1  + ","); // 2
    System.out.print(((A)this).s1); // 3
  }
  public static void main (String[] args) {
    new D().m1(); // 4
  }
}

What is the result of attempting to compile and run the above program?

a. Prints: D,D,D,D
b. Prints: D,C,B,A
c. Compiler Error at 1.
d. Compiler Error at 2.
e. Compiler Error at 3.
f. Compiler Error at 4.
g. Runtime Error
h. None of the Above


Copyright © 2002, Dan Chisholm
All rights reserved.
1